package controller;

import java.util.List;
 
 
import org.hibernate.HibernateException;
import org.hibernate.Session;
 
import model.Contact;
import util.HibernateUtil;
 
		

public class ContactManager extends HibernateUtil {
 
    public Contact add(Contact contact) {
        
    	Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        try {
        session.save(contact);
        }
        catch (HibernateException e) {
            e.printStackTrace();
            session.getTransaction().rollback();
        }
        session.getTransaction().commit();
        return contact;
    }
    
    public Contact delete(Long id) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        Contact contact = (Contact) session.load(Contact.class, id);
        if(null != contact) {
            
        	try {
        	session.delete(contact);
        	}
        	catch (HibernateException e) {
                e.printStackTrace();
                session.getTransaction().rollback();
            }
        }
        session.getTransaction().commit();
        return contact;
    }
    
    public Contact load(Long id) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        Contact contact = (Contact) session.get(Contact.class, id); // load(Contact.class, id);
        session.getTransaction().commit();
        return contact;
    }
    
    public void closeSession() {
    	Session session = getSessionFactory().getCurrentSession();
      	session.beginTransaction();
    	try {
      	session.getTransaction().commit();
       	//session.beginTransaction();	
    	//session.getTransaction().commit();
    	} catch (HibernateException e) {
            e.printStackTrace();
            session.getTransaction().rollback();
        }
       
    	return;
    }
    
    
    public Contact update(Contact contact) {
    	Session session = getSessionFactory().getCurrentSession();
        session.getTransaction().begin();
    	
    	try {
           	session.update(contact);
        }
        catch (HibernateException e) {
            e.printStackTrace();
            session.getTransaction().rollback();
        }
        session.getTransaction().commit();
        return contact;
    }
    
        
	@SuppressWarnings("unchecked")
	public List<Contact> list() {
         
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        List<Contact> contacts = null;
        try {
             
            contacts = (List<Contact>)session.createQuery("from Contact").list();
             
        } 
        catch (HibernateException e) {
            e.printStackTrace();
            session.getTransaction().rollback();
        
        }
        session.getTransaction().commit();
        return contacts;
    }
}
